package com.example.springbooteasycode.utils;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class JarHashComparator {
    public static void main(String[] args) throws Exception {
        String jar1Path = "/Users/boykchen/Desktop/新老包对比/老/app-cms-Old.jar";
        String jar2Path = "/Users/boykchen/Desktop/新老包对比/新/app-cms-New.jar";

        String hash1 = getHash(jar1Path);
        String hash2 = getHash(jar2Path);

        System.out.println("Hash of jar1: " + hash1);
        System.out.println("Hash of jar2: " + hash2);

        if (hash1.equals(hash2)) {
            System.out.println("The JAR files are identical.");
        } else {
            System.out.println("The JAR files are different.");
        }
    }

    private static String getHash(String filePath) throws Exception {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] bytes = Files.readAllBytes(Paths.get(filePath));
        byte[] hash = digest.digest(bytes);
        return bytesToHex(hash);
    }

    private static String bytesToHex(byte[] bytes) {
        StringBuilder result = new StringBuilder();
        for (byte b : bytes) {
            result.append(String.format("%02x", b));
        }
        return result.toString();
    }
}
